class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int len = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            len++;
        }
        int ret = len - n - 1;
        if (ret == -1) {
            return head.next;
        }
        cur = head;
        for (int i = 0; i < ret; i++) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return head;

    }
}